layui.config({
    base: ctx+'/plugin/authtree/',
}).extend({
    authtree: 'authtree',
}).use(['table', 'layer','form','authtree'], function(){
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var authtree = layui.authtree;
    var $ =layui.jquery;

    //创建角色数据列表
    var createRoleTable = function(){
        table.render({
            elem: '#roleFilter',
            toolbar : '#toolbar',
            defaultToolbar :[]
			,height : 600
            ,page: true
            ,cols:  [[
                {field: 'id', title: 'id',hide :true}
                ,{field: 'name', title: '角色名称', width: '10%',align : 'center'}
                ,{field: 'roleDesc', title: '角色标志符', width: '10%',align : 'center'}
                ,{field: 'seq', title: '角色权重', width: '10%',align : 'center'}
                ,{field: 'remark', title: '备注',width: '15%',align : 'center'}
                ,{field: 'operator', title: '操作人', width: '10%',align : 'center'}
                ,{field: 'operateTime', title: '操作时间', width: '15%',align : 'center'}
                ,{field: 'status', title: '状态', width: '10%',align : 'center',templet : function(row){
                        if(row.status == 1){
                            return "<span class='layui-badge layui-bg-blue'>激活</span>";
                        }else if(row.status == 0){
                            return "<span class='layui-badge layui-bg-gray'>冻结</span>";
                        }
                    }}
                ,{field: 'ops', title: '操作', width: '25%',align : 'center',templet : '#tool'}
            ]]
            ,url : ctx+"/rbac/role/list"
        });
    }

    createRoleTable();

    // 行工具条
    table.on('tool(roleFilter)',function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent == 'changeStatus'){
            layer.confirm('你确定要改变状态吗', function(index){
                commons.ajax({
                    url : ctx+'/rbac/role/onOrOff',
                    data : {
                        'id' : data.id
                    },
                    success : function(res){
                        layer.msg(res.msg);
                        layer.close(index);
                        table.reload('roleFilter');
                    },
                    fail : function(res){
                        layer.msg(res.msg);
                        layer.close(index);
                    }
                });

            });

        }else if(layEvent == 'edit'){
            edit(data);
        }else if(layEvent == 'acledit'){
            aclEdit(data);
        }
    })

    var index ;
    // 编辑操作
    function edit(data){
        // 回显数据
        form.val("editForm", {
            'id' : data.id,
            'name' : data.name,
            'remark' : data.remark,
            'seq' : data.seq
        });
        index =  layer.open({
            title : '角色信息修改',
            type: 1,
            content : $('#edit')
        });
    }

    // 提交更新信息
    form.on('submit(submit)',function(data){
        commons.ajax({
            url : ctx + '/rbac/role/update',
            data : data.field,
            success : function(res){
                layer.msg(res.msg);
                table.reload('roleFilter');
                layer.close(index);
            },
            fail : function(res){
                layer.msg(res.msg);
                layer.close(index);
            }
        });
        return false;
    });

    table.on('toolbar(roleFilter)', function(obj){
        var layEvent = obj.event;
        if(layEvent == 'add'){
            addRole();
        }
    });

    //新增角色
    function addRole(){
        layer.open({
            title : '新增角色信息',
            type: 1,
            content: $('#add')
        });
    }
    //提交新增信息
    form.on('submit(add)',function(data){
        var loadingIndex = layer.load(2);
        commons.ajax({
            url : ctx + '/rbac/role/add',
            data : data.field,
            success : function(res){
                layer.msg(res.msg);
                table.reload('roleFilter');
                layer.closeAll();
            },
            fail : function(res){
                layer.msg(res.msg);
                layer.close(index);
            }
        });
        return false;
    });

    // 权限维护
    function aclEdit(data){
        // 初始化当前用户的权限树
        commons.ajax({
            url: ctx+'/rbac/roleAcl/list',
            data : {
                'id' : data.id
            },
            success: function(res){
                // 打开弹出层
                layer.open({
                    title : '角色权限分配',
                    type: 1,
                    content : $('#authTree'),
                    //area: ['800px', '800px'],
                    btn : ['确定','取消'],
                    yes: function(index, layero){
                        var checked = authtree.getChecked('#LAY-auth-tree-index'); // 所有选中的节点内容
                        var aclIds = '';
                        $(checked).each(function(i,val){
                            aclIds += val + ",";
                        });
                        aclIds = aclIds.substring(0,aclIds.length-1);
                        var loadingIndex = layer.load(2);
                        commons.ajax({
                            url : ctx+'/rbac/roleAcl/allocate',
                            data : {
                                'roleId' : data.id,
                                'aclIds' : aclIds
                            },
                            success : function(res){
                                layer.msg(res.msg);
                                layer.close(loadingIndex);
                                layer.close(index);
                            }
                        });
                    }
                    ,btn2: function(index, layero){
                        layer.close(index);
                    }
                });

                var trees = authtree.listConvert(res.data.list, {
                    primaryKey: 'id'
                    ,startPid: 0
                    ,parentKey: 'parentId'
                    ,nameKey: 'name'
                    ,valueKey: 'id'
                    ,checkedKey: res.data.checkedId
                });

                authtree.render('#LAY-auth-tree-index', trees, {
                    inputname: 'authids[]',
                    layfilter: 'lay-check-auth',
                    autochecked: false,
                    openall : false,
                    openchecked : false
                });
            }
        });

    }

});